package tech.aistar.controller.notecom;

import org.json.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.entity.notecom.Com;
import tech.aistar.service.notecom.IComService;
import tech.aistar.util.IdWorker;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by Administrator on 2019/10/9.
 */
@Controller
@RequestMapping("/send")
@SuppressWarnings("all")
public class SendComAction {
    @Autowired
    private IComService comService;
    /**
     * 雪花id生成器
     */
    @Autowired
    private IdWorker idWorker;

    /**
     * 插入一级评论
     * 模拟用户id:1
     * @param noteId
     * @param content
     * @return
     */
    @PostMapping("/sendComOne")
    @ResponseBody
    public Com sendComOne(Model model, Integer noteId, String content, Integer userId){

        Integer floorNum = comService.findOneComNum(noteId);

        Com com = new Com();
        //楼层数+1
        com.setFloor(floorNum.intValue()+1);
        com.setNoteId(noteId);
        com.setUserId(userId);
        com.setContent(content);
        com.setCtime(new Date());
        com.setPointNum(6);
        com.setFlag(1);
        comService.saveComment(com);

        return com;
    }

    /**
     * 插入二级评论
     * 模拟用户id:2
     * @param noteId
     * @param content
     * @return
     */
    @RequestMapping("/sendComTwo")
    @ResponseBody
    public Com sendComTwo(HttpSession session, Integer noteId, String content, Integer userId, Integer floor){

        UserTable userTable = (UserTable) session.getAttribute("userId");
        Integer integer = userTable.getId();

        Com com = new Com();
        com.setFloor(floor);
        com.setNoteId(noteId);
        com.setUserId(integer);
        com.setToUserId(userId);
        com.setContent(content);
        com.setCtime(new Date());
        com.setPointNum(6);
        com.setFlag(2);
        comService.saveComment(com);
//        String a = String.valueOf(noteId);
//        return "redirect:/com/comment?noteId="+1;
        return com;
    }

    /**
     * 插入三级评论
     * 模拟用户id:3
     * @param noteId
     * @param content
     * @return
     */
    @RequestMapping("/sendComThree")
    @ResponseBody
    public Com sendComThree(HttpSession session,Integer noteId, String content, Integer userId, Integer floor){
        UserTable userTable = (UserTable) session.getAttribute("userId");
        Integer integer = userTable.getId();

        Com com = new Com();
        com.setFloor(floor);
        com.setNoteId(noteId);
        com.setUserId(integer);
        com.setToUserId(userId);
        com.setContent(content);
        com.setCtime(new Date());
        com.setPointNum(6);
        com.setFlag(2);
        comService.saveComment(com);

        return com;
    }
}
